import { IRouter, IMediator, IRoute, IValid } from "../interfaces";
import { AppEvent } from "../event/AppEvent";
import { AppEventName } from "../Define";
import { Route } from "./Route";
import { EventEmitter } from "../event/EventEmitter";

export class Router extends EventEmitter implements IRouter, IValid {
    isValid() {
        return true;
    }
    constructor(mediator: IMediator) {
        super(mediator);
    }
    init(): void {

        this._mediator.subscribe(new AppEvent(AppEventName.hashchange, (data: IRoute) => {
            // let r = this.getRoute();
            this.onRouteChange(data)
        }, this))

        this.subscribeEvents([
            new AppEvent(AppEventName.app_init, (data: any) => {

            }, this),
            new AppEvent(AppEventName.app_route, (data: any) => {
                this.setRoute(data)
            }, this)

        ])
    }

    // private getRoute(): IRoute {
    //     let h = ""
    //     return this.parseRute(h)
    // }

    private setRoute(route: IRoute) {
        // let s = route.serialize();
        // window.location.hash = s;
    }

    // private parseRute(h: string): IRoute {
    //     return new Route('', '', null)
    // }

    private onRouteChange(route: Route) {
        this.triggerEvent(AppEventName.app_dispatch, route)
    }
}